package com.ruoyi.demo04;


import java.util.List;
import java.util.Random;

/**
 * @Author Husp
 * @Date 2023/12/10 17:24
 */
public class EmployeeThread extends Thread {

    private List<Integer> moneys;

    private int totalMoney;

    public EmployeeThread(List<Integer> moneys, String threadName) {
        super(threadName);
        this.moneys = moneys;
    }

    private Random r = new Random();

    @Override
    public void run() {
        //100个人抢红包
        while (true) {
            String threadName = Thread.currentThread().getName();
            synchronized (moneys) {
                //判断红包有没有钱了
                if (moneys.size() == 0) {
                    break;
                }
                int money = moneys.remove(r.nextInt(moneys.size()));
                System.out.println(threadName + "抢到了" + money + "元");
                totalMoney += money;
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException("当前线程发生了异常！");
                }
            }
        }
    }

    public Integer getTotalMoney() {
        return totalMoney;
    }

    public void setTotalMoney(Integer totalMoney) {
        this.totalMoney = totalMoney;
    }
}
